﻿using System;
using System.Diagnostics;
using System.IO;
using System.IO.IsolatedStorage;
using System.Xml.Serialization;
using Exception = System.Exception;

namespace TaskSync2010
{
    public static class UserSettings
    {
        private static readonly Data Data = Data.Load();

        public static string Username
        {
            get { return Data.Username; }
            set { Data.Username = value; }
        }

        public static string Password
        {
            get { return Data.Password; }
            set { Data.Password = value; }
        }

        public static string ProxyUsername
        {
            get { return Data.ProxyUsername; }
            set { Data.ProxyUsername = value; }
        }

        public static string ProxyPassword
        {
            get { return Data.ProxyPassword; }
            set { Data.ProxyPassword = value; }
        }

        public static string ProxyServer
        {
            get { return Data.ProxyServer; }
            set { Data.ProxyServer = value; }
        }

        public static int ProxyPort
        {
            get { return Data.ProxyPort; }
            set { Data.ProxyPort = value; }
        }

        public static int Interval
        {
            get { return Data.Interval; }
            set { Data.Interval = value; }
        }

        public static bool ProxyInUse
        {
            get { return Data.ProxyInUse; }
            set { Data.ProxyInUse = value; }
        }

        public static void Save()
        {
            Data.Save();
        }
    }

    public class Data
    {
        public string Username { get; set; }
        public string Password { get; set; }        
        public string ProxyUsername { get; set; }
        public string ProxyPassword { get; set; }
        public string ProxyServer { get; set; }
        public int ProxyPort { get; set; }
        public int Interval { get; set; }
        public bool ProxyInUse { get; set; }

        private static readonly IsolatedStorageFile Store = IsolatedStorageFile.GetUserStoreForAssembly();
        private const string Filename = "usersettings.xml";

        public static Data Load()
        {
            Data newdate = null;
            IsolatedStorageFileStream stream = null;
            if (Store.FileExists(Filename))
            {
                try
                {
                    stream = new IsolatedStorageFileStream(Filename, FileMode.Open, Store);
                    XmlSerializer serializer = new XmlSerializer(typeof(Data));
                    newdate = serializer.Deserialize(stream) as Data;
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e.Message);
                }
                finally
                {
                    if (stream != null) stream.Close();
                }
            }
            return newdate ?? new Data();
        }

        public void Save()
        {            
            //assureDirectoryExists<ItemType>();//todo exception handling
            XmlSerializer serializer = new XmlSerializer(typeof(Data));
            IsolatedStorageFileStream stream = new IsolatedStorageFileStream(Filename, FileMode.Create, Store);
            serializer.Serialize(stream, this);
            stream.Close();
        }        

    }
        
}
